package com.ujcms;

import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.ujcms.cms.core.support.Props;
import com.ujcms.commons.image.ImageHandler;
import com.ujcms.commons.image.ThumbnailatorHandler;
import com.ujcms.commons.misc.LongArrayToStringSerializer;
import com.ujcms.commons.web.PathResolver;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;

import jakarta.servlet.ServletContext;

/**
 * 根据 <a href="https://start.spring.io/">...</a> 生成的代码范例。
 * <p>
 * SpringBoot的入口和主要配置文件
 *
 * @author PONY
 */
@MapperScan(basePackages = {"com.ujcms.cms.core.mapper", "com.ujcms.cms.ext.mapper"})

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class,
        MongoAutoConfiguration.class})
public class Application extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    /**
     * UJCMS 配置
     */
    @Bean
    public Props props() {
        return new Props();
    }

    /**
     * 真实路径获取组件
     */
    @Bean
    public PathResolver pathResolver(ObjectProvider<ServletContext> servletContextProvider) {
        return new PathResolver(servletContextProvider);
    }

    /**
     * 图片处理组件
     */
    @Bean
    public ImageHandler imageHandler() {
        return new ThumbnailatorHandler();
    }

    /**
     * JSON序列号：long转string
     */
    @Bean
    public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
        return jacksonObjectMapperBuilder -> jacksonObjectMapperBuilder
                .serializerByType(Long.class, ToStringSerializer.instance)
                .serializerByType(Long.TYPE, ToStringSerializer.instance)
                .serializerByType(long[].class, LongArrayToStringSerializer.INSTANCE);
    }
}